<?php

class Application_Model_RmaMapper {
	
	protected $_connection;

	private function _connect()
	{
		$this->_connection = Zend_Db_Table::getDefaultAdapter();
	}
	
	public function getRmaStatusById($id)
	{
		$db = new Application_Model_DbTable_RmaRequest();
		
		$sql = $db->select()
					->from('tbl_rmarequest', 'status')
					->where('id='.$id);
		
		$result = $sql->query()->fetch();
		
		return $result;
	}
	
	public function fetchAllRmaRequests()
	{
		$this->_connect();
		
		$subSql = $this->_connection->select()
										->from(array('ri' => 'bis.tbl_rmarequestitems'), array('ri.rma_id', 'quantity' => 'SUM(ri.rmaQuantity)'))
										->group('ri.rma_id');							
		
		$sql = $this->_connection->select()
						->from(array('r' => 'bis.tbl_rmarequest'))
						->joinLeft(array('c' => 'bis.tbl_customer'), 'c.partyNumber=r.partyNumber', 'c.customerName')
						->joinLeft(array('m' => 'bis.tbl_mshData'), 'm.SAPCODE=r.sapCode AND m.status=\'A\'', 'm.outletName')
						->joinLeft(array('ri' => $subSql), 'r.id=ri.rma_id', 'ri.quantity');
		
		$result = $sql->query()->fetchAll();
		
		return $result;
	}
}